package com.example.demo.easy.controller;

import com.alibaba.fastjson.JSON;
import com.example.demo.easy.entity.UserInfo;
import com.example.demo.easy.service.UserInfoService;
import org.json.JSONException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (UserInfo)表控制层
 *
 * @author makejava
 * @since 2021-07-19 17:41:13
 */
@RestController
@RequestMapping(value = "userInfo")
public class UserInfoController {
    /**
     * 服务对象
     */
    @Resource
    private UserInfoService userInfoService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @PostMapping("selectOne")
    public UserInfo selectOne(@RequestParam (name = "id")Integer id) {
        UserInfo userInfo=this.userInfoService.queryById(id);
        return userInfo;
    }

    @GetMapping("selectTne")
    public UserInfo selectTne(@RequestParam (name = "id")Integer id) {
        UserInfo userInfo=this.userInfoService.queryById(id);
        return userInfo;
    }
    @GetMapping("selectAll")
    @CrossOrigin
    public  List<UserInfo> selectAll() {
        StringBuilder stringBuilder=new StringBuilder();
        List<UserInfo> list=this.userInfoService.queryAllByLimit(1,100);
//        for (UserInfo userInfo :list) {
//            stringBuilder.append(userInfo.toString());
//
//        }
        return list;
    }
    @GetMapping("selectAllJson")
    @CrossOrigin
    public  String selectAllJson() throws JSONException {
        List<UserInfo> list=this.userInfoService.queryAllByLimit(1,100);
        String str = JSON.toJSONString(list); // List转json

            return str;


    }
    @GetMapping("insertUser")
    public UserInfo insertUser(@RequestParam (name = "id")Integer id,
                               @RequestParam (name = "name")String name,
                               @RequestParam (name = "age")String age) {

        UserInfo userInfo=new UserInfo();
        userInfo.setId(id);
        userInfo.setName(name);
        userInfo.setAge(age);
        this.userInfoService.insert(userInfo);
        return userInfo;
    }

}
